// packageB/pages/package/smart_box/search/self/index.js
const mybehavior = require("../../../../../../behaviors/requests")
Page({

    /**
     * 页面的初始数据
     */
    behaviors: [mybehavior],
    data: {
        baseUrl: "https://logisticsengineering.gree.com/smartg_api/material_manage",
        button_style1: "button_style1_selected",
        button_style2: "button_style2",
        daiqu_flag: true,
        yiqu_flag: false,
        data_list: [],
        data_list_yiqu: [



        ],
        page1: 1,
        page2: 1,
        pagesize: 3,
        parameter: "",
        total1: 0,
        total2: 0,

    },
    selectSearch() {
        if (this.data.daiqu_flag) {
            this.getDaiQuDatasSearch()
        } else {
            this.getYiQuDatasSearch()
        }
    },

    daiquBtn() {
        if (!this.data.daiqu_flag) {
            this.setData({
                button_style1: "button_style1_selected",
                button_style2: "button_style2",
                daiqu_flag: true,
                parameter: "",
                yiqu_flag: false,
            })
        }


    },
    yiquBtn() {
        if (!this.data.yiqu_flag) {
            this.setData({
                button_style1: "button_style1",
                button_style2: "button_style2_selected",
                daiqu_flag: false,
                parameter: "",
                yiqu_flag: true,
            })
        }

    },
    getDaiQuDatasSearch() {
        this.setData({
            total1: 0,
            page1: 1,
            data_list: []
        })
        this.getDaiQuDatas()
    },
    // 获取待取件数据
    getDaiQuDatas() {
        let url = this.data.baseUrl + "/get_materials"
        let data = {
            page: this.data.page1,
            pagesize: this.data.pagesize,
            parameter: this.data.parameter,
        }
        this.post(url, data).then(res => {

            if (res.code == 20000) {
                let array = res.data.results
                let arr_list = []
                arr_list = this.data.data_list.concat(array)
                this.setData({
                    data_list: arr_list,
                    total1: res.data.total
                })

                wx.hideLoading()
            } else {
                wx.showToast({
                    title: res.msg,
                    icon: 'none'
                })
            }
        }).catch(err => {
            this.setData({
                data_list: [],
                total1: 0
            })
            wx.showToast({
                title: err.msg,
                icon: 'none'
            })
        })


    },
    getYiQuDatasSearch() {
        this.setData({
            total2: 0,
            page2: 1,
            data_list_yiqu: []
        })
        this.getYiQuDatas()
    },
    // 获取已取件数据
    getYiQuDatas() {
        let url = this.data.baseUrl + "/get_picked_up_materials"
        let data = {
            page: this.data.page2,
            pagesize: this.data.pagesize,
            parameter: this.data.parameter,

        }
        this.post(url, data).then(res => {

            if (res.code == 20000) {
                let array = res.data.results
                let arr_list = []
                arr_list = this.data.data_list_yiqu.concat(array)
                for (let i = 0; i < arr_list.length; i++) {
                    arr_list[i].out_time = this.GMTToStr(arr_list[i].out_time)
                }
                this.setData({
                    data_list_yiqu: arr_list,
                    total2: res.data.total
                })
                wx.hideLoading()

            } else {
                wx.showToast({
                    title: res.msg,
                    icon: 'none'
                })
            }
        }).catch(err => {
            console.log(err);
            this.setData({
                data_list_yiqu: [],
                total2: 0
            })
            wx.showToast({
                title: err.msg,
                icon: 'none'
            })
        })


    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        this.getDaiQuDatas()
        this.getYiQuDatas()
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {
        if (this.data.daiqu_flag) {
            wx.showLoading({
                title: '加载中..'
            })
            const e = this.data.page1 + 1
            if (Math.ceil(this.data.total1 / this.data.pagesize) >= e) {
                this.setData({
                    page1: e
                })
                this.getDaiQuDatas(e)
            } else {
                wx.showToast({
                    title: '已经到底啦~',
                    icon: 'none'
                })
            }
        } else {
            wx.showLoading({
                title: '加载中..'
            })
            const e = this.data.page2 + 1
            if (Math.ceil(this.data.total2 / this.data.pagesize) >= e) {
                this.setData({
                    page2: e
                })
                this.getYiQuDatas(e)
            } else {
                wx.showToast({
                    title: '已经到底啦~',
                    icon: 'none'
                })
            }
        }

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})